package com.wds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wds.common.utils.BeanCopyUtils;
import com.wds.domain.JsonResult;
import com.wds.domain.entity.Tag;
import com.wds.domain.vo.TagListVo;
import com.wds.domain.vo.TagListVoVo;
import com.wds.domain.vo.TagQueryByIdVo;
import com.wds.exception.CustomException;
import com.wds.mapper.TagMapper;
import com.wds.service.TagService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 标签(Tag)表服务实现类
 *
 * @author wds
 * @since 2022-12-05 11:51:41
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Override
    public JsonResult<List<Tag>> queryAll() {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Tag::getUpdateTime);
        List<Tag> list = list(queryWrapper);
        return JsonResult.ok(list);
    }

    @Override
    public JsonResult<TagListVoVo> pageList(int pageNum, int pageSize, String name) {
        Page<Tag> tagPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null && !("".equals(name)),Tag::getName, "%" + name + "%");
        tagPage = page(tagPage, queryWrapper);
        List<TagListVo> tagListVos = BeanCopyUtils.copyList(tagPage .getRecords(), TagListVo.class);
        TagListVoVo tagListVoVo = new TagListVoVo();
        tagListVoVo.setRows(tagListVos);
        tagListVoVo.setTotal(tagPage.getTotal());
        return JsonResult.ok(tagListVoVo);
    }

    @Override
    public JsonResult<Object> addNewTag(Tag tag) {
        if (!StringUtils.hasText(tag.getName())) {
            throw new CustomException("请传入标签名");
        }

        Tag dto = new Tag();
        dto.setName(tag.getName());
        dto.setRemark(tag.getRemark());
        save(dto);
        return JsonResult.ok();
    }

    @Override
    public JsonResult<Object> delete(List<Long> id) {
        removeByIds(id);
        return JsonResult.ok();
    }

    @Override
    public JsonResult<Object> queryById(String id) {
        Tag tag = getById(id);
        TagQueryByIdVo idVo = BeanCopyUtils.copy(tag, TagQueryByIdVo.class);
        return JsonResult.ok(idVo);
    }

    @Override
    public JsonResult<Object> updateTag(Tag tag) {
        LambdaUpdateWrapper<Tag> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Tag::getId, tag.getId());
        updateWrapper.set(StringUtils.hasText(tag.getName()), Tag::getName, tag.getName());
        updateWrapper.set(StringUtils.hasText(tag.getRemark()), Tag::getRemark, tag.getRemark());
        update(updateWrapper);

        return JsonResult.ok();
    }
}

